package top.kaoshanji.p1ch02d;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
 * 代码清单 p1ch02d-04：MethodHandles#dropArguments的使用示例
 * author kaoshanji
 * ^_^
 * create time 2025/1/3 18:15
 */
public class MethodHandlesExample04 {

    private static final Logger logger = LogManager.getLogger(MethodHandlesExample04.class);

    public static void main(String[] args) throws Throwable {
        logger.info("...代码清单 p1ch02d-04：MethodHandles#dropArguments的使用示例...");

        MethodType type = MethodType.methodType(String.class, int.class, int.class);
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle mhOld = lookup.findVirtual(String.class, "substring", type);
        String value = (String) mhOld.invoke("Hello", 2, 4);
        System.out.println(value);

        MethodHandle mhNew = MethodHandles.dropArguments(mhOld, 0, float.class, String.class);
        value = (String) mhNew.invoke(0.5f, "Ignore", "Hello", 1, 3);

        System.out.println(value);

    }

}
